package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Flag;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult<Emp> queryEmp(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        Page<Emp> emps = (Page<Emp>) empMapper.findAll(name, gender, begin, end);

        PageResult<Emp> pageResult = new PageResult<>();
        pageResult.setTotal(emps.getTotal());
        pageResult.setRows(emps.getResult());
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Emp emp) throws Exception {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.add(emp);


//        将取出的主键赋值给Empexpr里的empId
            Integer empId = emp.getId();
            List<EmpExpr> empExpr = emp.getExprList();

//        如果empExpr不为空，则进行遍历
            if (empExpr != null && !empExpr.isEmpty()) {
                empExpr.forEach(empExpr1 -> empExpr1.setEmpId(empId));
                empExprMapper.addEmpExpr(empExpr);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
            empLogService.insertLog(empLog);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer[] ids) {
//        删除员工
        empMapper.delete(ids);
//        删除员工经历
        empExprMapper.delete(ids);
    }


    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Emp emp) {
//        补全属性
        emp.setUpdateTime(LocalDateTime.now());
//        修改员工基本信息
        empMapper.update(emp);
//        删除工作经历
        empExprMapper.delete(new Integer[]{emp.getId()});
//        添加工作经历
        List<EmpExpr> empExpr = emp.getExprList();
        if (empExpr != null && !empExpr.isEmpty()) {
            empExpr.forEach(empExpr1 -> empExpr1.setEmpId(emp.getId()));
            empExprMapper.addEmpExpr(empExpr);
        }
    }


    @Override
    public List<Emp> list() {
        return empMapper.list();
    }

    @Override
    public JobOption jobData() {
        List<Map<String, Object>> jobOption = empMapper.jobData();

        List<Object> jobList = jobOption.stream().map((Map<String, Object> map) -> {
            return map.get("job");
        }).collect(Collectors.toList());

        List<Object> numberList = jobOption.stream().map((Map<String, Object> map) -> {
            return map.get("number");
        }).collect(Collectors.toList());
        JobOption jobOption1 = new JobOption(jobList, numberList);

        return jobOption1;

    }

    @Override
    public List<Map<String, Object>> empGenderData() {
        return empMapper.empGenderData();
    }

    @Override
    public List<Map<String, Object>> studentDegreeData() {
        return empMapper.studentDegreeData();
    }

    @Override
    public ClazzOption studentCountData() {
        List<Map<String, Object>> maps = empMapper.studentCountData();
        List<Object> clazzList = maps.stream().map((Map<String, Object> map) -> {
            return map.get("clazzList");
        }).collect(Collectors.toList());
        List<Object> dataList = maps.stream().map((Map<String, Object> map) -> {
            return map.get("dataList");
        }).collect(Collectors.toList());
        ClazzOption clazzOption = new ClazzOption(clazzList, dataList);
        return clazzOption;
    }
}
